﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Autorun
{
    public partial class PaneSchedules : Autorun.PaneBase
    {
        #region "Constructors & Overriden methods"
        public PaneSchedules()
        {
            InitializeComponent();
        }

        public override bool Initialize(Autorun.IPresenter sender, Autorun.ShowPaneEventArgs e)
        {

            _parent = sender;
            _parent.OnChangingPane += new ChangePaneEventHandler(_parent_OnChangingPane);

            if ((e != null) && (e.Args != null) && (e.Args.Length != 0))
                if (e.Args[0] is ServerManagement)
                    _proxy = (ServerManagement)e.Args[0];
                else
                    _proxy = null;
            else
                _proxy = null;

            RefreshInformation();

            return true;
        }
        #endregion

        #region "Procedures"
        private void RefreshInformation()
        {
            ListViewItem lvi;

            myList.Items.Clear();

            if (_proxy != null)
            {
                Caption = string.Format("{0}: List of schedules", _proxy.Information.MachineName);

                foreach (ScheduleItem item in _proxy.Scheduler.Items)
                {
                    lvi = myList.Items.Add(item.Caption, 0);
                    lvi.Tag = item.Id;
                    switch (item.Types)
                    {
                        case ScheduleTypes.Inactive:
                            lvi.Group = myList.Groups[0];
                            break;
                        case ScheduleTypes.Startup:
                            lvi.Group = myList.Groups[1];
                            break;
                        case ScheduleTypes.Shutdown:
                            lvi.Group = myList.Groups[2];
                            break;
                        case ScheduleTypes.Daily:
                            lvi.Group = myList.Groups[3];
                            break;
                        case ScheduleTypes.Weekly:
                            lvi.Group = myList.Groups[4];
                            break;
                        case ScheduleTypes.Monthly:
                            lvi.Group = myList.Groups[5];
                            break;
                        default:
                            throw new Exception();
                    }
                    lvi.SubItems.Add(item.StartDateTime.ToString("dd/MM/yyyy"));
                    lvi.SubItems.Add(item.FinishDate.Equals(DateTime.MaxValue) ? "-" : item.FinishDate.ToString("dd/MM/yyyy"));
                    lvi.SubItems.Add(item.RepeatEvery.ToString());
                    //lvi.SubItems.Add(item.IsUsed.ToString());
                    //lvi.SubItems.Add(item.Path);
                }
                myList.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                myList.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                myList.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                //myList.Columns[3].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                //myList.Columns[4].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
            else
                Caption = "Not connected";

        }
        #endregion


        private void mnuNew_Click(object sender, EventArgs e)
        {
            ScheduleDialog dlg;

            Cursor = Cursors.WaitCursor;
            dlg = new ScheduleDialog();
            dlg.SetData(_proxy, 0);
            Cursor = Cursors.Default;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                //    DoSomething();
                RefreshInformation();
            }
        }

        private void mnuDelete_Click(object sender, EventArgs e)
        {
            DialogResult rc;

            rc = MessageBox.Show(string.Format("Do you want to remove the schedule '{0}'?", myList.SelectedItems[0].SubItems[0].Text), Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            switch (rc)
            {
                case DialogResult.Yes:
                    _proxy.Scheduler.Delete((int)myList.SelectedItems[0].Tag);
                    RefreshInformation();
                    break;
                case DialogResult.No:
                case DialogResult.Cancel:
                    break;
                default:
                    throw new Exception("Unknown Dialog Result");
            }
        }

        private void mnuEdit_Click(object sender, EventArgs e)
        {
            ScheduleDialog dlg;

            if (myList.SelectedItems.Count != 0)
            {
                dlg = new ScheduleDialog();
                dlg.SetData(_proxy, (int)myList.SelectedItems[0].Tag);
                if (dlg.ShowDialog() == DialogResult.OK)
                    RefreshInformation();
            }
        }
    }
}
